<template>
  <div class="performance-list-container">
    <!-- 头部 -->
    <div class="performance-header">
      <el-button 
        circle 
        @click="goBack"
        class="back-btn"
      >
        <img :src="backIcon" alt="返回" class="back-icon" />
      </el-button>
      <h2 class="header-title">演出列表</h2>
      <div class="header-right"></div>
    </div>

    <!-- 演出列表 -->
    <div class="performance-list">
      <div 
        v-for="performance in filteredPerformances" 
        :key="performance.id"
        class="performance-item"
        @click="viewPerformance(performance)"
      >
        <div class="performance-avatar">
          <el-avatar 
            :size="60"
            :src="performance.artistAvatar"
            :icon="performance.artistAvatar ? undefined : 'User'"
          />
        </div>
        <div class="performance-info">
          <h3 class="artist-name">{{ performance.artistName }}</h3>
          <p class="performance-time">{{ performance.timeText }}</p>
        </div>
        <div class="performance-arrow">
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div>
      
      <!-- 空状态 -->
      <div v-if="filteredPerformances.length === 0" class="empty-state">
        <el-icon size="60" color="#ccc"><Calendar /></el-icon>
        <p>暂无演出信息</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowRight, Calendar } from '@element-plus/icons-vue'
import { getPerformanceList } from '@/api/performance'
import type { Performance } from '@/types'

// 扩展Performance类型以包含显示所需的属性
interface PerformanceWithDisplay extends Performance {
  timeText: string;
  artistName: string;
  artistAvatar?: string;
}

// 导入图片资源
import backIcon from '@/assets/images/BACK.png'

// 响应式数据
const performances = ref<PerformanceWithDisplay[]>([])
const loading = ref(false)

// 获取路由
const router = useRouter()

// 计算属性 - 过滤今天当前时间之后的演出
const filteredPerformances = computed(() => {
  const now = new Date()
  const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
  
  return performances.value.filter(performance => {
    const performanceDate = new Date(performance.startTime)
    return performanceDate >= now
  }).sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime())
})

// 返回主页
const goBack = () => {
  router.push('/')
}

// 查看演出详情
const viewPerformance = (performance: PerformanceWithDisplay) => {
  router.push(`/performances/${performance.id}`)
}

// 格式化时间显示
const formatTimeText = (performance: Performance) => {
  const startTime = new Date(performance.startTime)
  const now = new Date()
  const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
  const tomorrow = new Date(today.getTime() + 24 * 60 * 60 * 1000)
  
  const isToday = startTime >= today && startTime < tomorrow
  const isTomorrow = startTime >= tomorrow && startTime < new Date(tomorrow.getTime() + 24 * 60 * 60 * 1000)
  
  const timeStr = startTime.toLocaleTimeString('zh-CN', { 
    hour: '2-digit', 
    minute: '2-digit',
    hour12: false 
  })
  
  if (isToday) {
    return `今日${timeStr}-${performance.venue}`
  } else if (isTomorrow) {
    return `明日${timeStr}-${performance.venue}`
  } else {
    return `${startTime.toLocaleDateString('zh-CN', { month: '2-digit', day: '2-digit' })}${timeStr}-${performance.venue}`
  }
}

// 获取演出列表
const fetchPerformances = async () => {
  try {
    loading.value = true
    const response = await getPerformanceList()
    console.log(response)
    // 处理演出数据，添加时间显示文本和艺人信息
    performances.value = response.data.items.map((performance: any): PerformanceWithDisplay => ({
      ...performance,
      timeText: formatTimeText(performance),
      artistName: performance.artist?.name || '未知艺人',
      artistAvatar: performance.artist?.avatar
    }))
  } catch (error) {
    console.error('获取演出列表失败:', error)
    ElMessage.error('获取演出列表失败')
  } finally {
    loading.value = false
  }
}

// 页面挂载时执行
onMounted(() => {
  fetchPerformances()
})
</script>

<style lang="scss" scoped>
.performance-list-container {
  min-height: 100vh;
  background: #2d2d2d;
  color: white;
}



.performance-header {
  background: #333;
  color: white;
  padding: 15px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.back-btn {
  background: transparent;
  border: none;
  color: white;
  
  &:hover {
    background: transparent;
  }
}

.back-icon {
  width: 20px;
  height: 20px;
  object-fit: contain;
  filter: brightness(0) invert(1);
  display: block;
  flex-shrink: 0;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  flex: 1;
  text-align: center;
}

.header-right {
  width: 40px;
}

.performance-list {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.performance-item {
  background: transparent;
  padding: 15px 0;
  display: flex;
  align-items: center;
  gap: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  
  &:hover {
    background: rgba(255, 255, 255, 0.05);
  }
  
  &:last-child {
    border-bottom: none;
  }
}

.performance-avatar {
  flex-shrink: 0;
}

.performance-info {
  flex: 1;
  
  .artist-name {
    font-size: 16px;
    font-weight: 600;
    color: #4CAF50;
    margin: 0 0 5px 0;
  }
  
  .performance-time {
    font-size: 14px;
    color: #ccc;
    margin: 0;
  }
}

.performance-arrow {
  flex-shrink: 0;
  color: #ccc;
  font-size: 16px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #999;
  
  p {
    margin: 15px 0 0 0;
    font-size: 16px;
  }
}

// 移动端适配
@media screen and (max-width: 768px) {
  .performance-header {
    padding: 12px 15px;
  }
  
  .header-title {
    font-size: 16px;
  }
  
  .back-icon {
    width: 26px;
    height: 26px;
  }
  
  .performance-list {
    padding: 15px;
  }
  
  .performance-item {
    padding: 12px 0;
    gap: 12px;
  }
  
  .performance-avatar {
    .el-avatar {
      width: 50px;
      height: 50px;
    }
  }
  
  .performance-info {
    .artist-name {
      font-size: 15px;
    }
    
    .performance-time {
      font-size: 13px;
    }
  }
}
</style> 